<?php 
//ArrowController::importModel("org.arrowplatform.interfaces.IArrowSingleton");
ArrowController::importModel("org.arrowplatform.controls.common.actions.ArrowAbstractUIAction");
class ArrowUIActionsManager  implements IArrowSingleton, IArrowControlsManager{

	
	private $registredActions = array(
		"link" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowLinkAction",
		"execute" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowExecuteAction",
		"popup" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowPopupAction",
		"dialog" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowDialogAction",
		"html" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowCustomHtmlAction",
		"create-popup" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowCreatePopupAction",
		"create-dialog" => "org.arrowplatform.controls.common.actions.uiActionsTypes.ArrowCreateDialogAction" 
	);
	
	
	/**
	 * Singleton static var
	 *
	 * @var ArrowUIActionsManager
	 */
	private static $selfInstance = null;
	
	/**
	 * Singleton
	 * 
	 * @return ArrowUIActionsManager
	 */
	public static function getDefault( ) {
		if (self :: $selfInstance == null ) {
			self :: $selfInstance = new ArrowUIActionsManager();
		}
		return self :: $selfInstance;
	}
	
	public function actionHandle( $params, $prefix, $name ){
		if( !isset($this->registredActions[$params["type"]] ) ) {
	        throw new ArrowCoreException( "UI Action type definition error[ type=".$params["type"]." not found ]"  ) ;
	    }
	    
		$model = $this->registredActions[$params["type"]];
		$class = explode(".", $model);
		$class = array_pop($class);
		ArrowController::importModel($model);
		
		$fieldObj = new $class( $params, $prefix, $name, ArrowView::getCurrentView()->getTemplateDescriptor()->getKey(), $class );
		return array( "model" => $model, "object"=>$fieldObj );
	}
	
	
	public function getControlVersionsConf($prefix,$tag){
		$tmp = array();
		foreach($this->registredActions as $param => $action){
			$obj = $this->actionHandle( array("type" => $param), $prefix, $tag  );
			$obj["object"]->configure();
			$params = array_merge( $obj["object"]->getProperties() ,array( "type" => $param ));
			$tmp["action:".$param] = array(
				"params" => $params,
				"requiredParams" => $obj["object"]->getRequiredProperties(),
				"default" => 'type="'.$param.'"'
			); 
		}
		return $tmp;
	}	
	
} 
?>